package com.sinosoft.application.webservice.util;

import java.util.Properties;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.sinosoft.application.webservice.client.PushApplicationService;

/**
 * WebService工具类
 * @author leichao
 *
 */
public class WSUtil {

	private static JaxWsProxyFactoryBean applicationProxy;	//	机构信息推送服务客户端代理
	
	private static String applicationServiceUrl;
	static{
		applicationServiceUrl = getApplicationParam("applicationServiceUrl");
	}
	
	private static Properties props;
	
	public static String getApplicationParam(String strParam) {
		//Properties props = null;
		if (props == null) {
			try {
				Resource resource = new ClassPathResource(
						"/web/conn.properties");
				props = PropertiesLoaderUtils.loadProperties(resource);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return props.getProperty(strParam);
	}
	
	/**
	 * 获取推送机构信息服务客户代理
	 * @return 构信息服务客户代理
	 */
	public static PushApplicationService getPushOrganizationClient() {
		Object service = createClientProxy(
				applicationProxy, applicationServiceUrl, PushApplicationService.class);
		return (PushApplicationService)service;
	}
	
	/**
	 * 创建客户端服务代理
	 * @param proxy	WebService代理类
	 * @param serviceUrl	服务地址
	 * @param serviceClass	接口服务类
	 * @return	客户端服务代理
	 */
	private static Object createClientProxy(JaxWsProxyFactoryBean proxy, String serviceUrl, Class<?> serviceClass) {
		if(proxy == null) {
			proxy = new JaxWsProxyFactoryBean();
			proxy.setAddress(serviceUrl);
			proxy.setServiceClass(serviceClass);
		}
		return proxy.create();
	}
}
